package diamondRingDao.impl;

import diamondRingDao.AddressDao;
import diamondRingEntity.Address;
import tool.BaseDao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class AddressDaoImpl extends BaseDao implements AddressDao {
    @Override
    public List<Address> addFind(int userid) {
        ResultSet rs=null;
        Address address=null;
        List<Address> addresses=new ArrayList<>();
        try {
            String sql="SELECT *\n" +
                    "FROM useraddress\n" +
                    "WHERE useraddress.userid=? and isDefault=1";
            Object[] param={userid};
            rs=this.executeQuery(sql,param);
            while (rs.next()){
                address=new Address();
                address.setAddressId(rs.getInt(1));
                address.setUserId(rs.getInt(2));
                address.setAddName(rs.getString(3));
                address.setDiamondProvince(rs.getString(4));
                address.setDiamondCity(rs.getString(5));
                address.setDiamondDistrict(rs.getString(6));
                address.setDiamondStreet(rs.getString(7));
                address.setAddPhone(rs.getString(8));
                address.setIsDefault(rs.getInt(9));
                addresses.add(address);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return addresses;
    }

    @Override
    public int addInsert(Address address) {
        int count=0;
        Object[] param={address.getUserId(), address.getAddName(), address.getDiamondProvince(),address.getDiamondCity(),address.getDiamondDistrict(), address.getDiamondStreet(), address.getAddPhone(), address.getIsDefault()};
        Object[] param2={address.getUserId()};
        String sql=null;
        if (address.getIsDefault()==1){
            String sql2="UPDATE useraddress SET useraddress.isDefault=0 WHERE useraddress.userid=?;";
            this.executeUpdate(sql2,param2);
            sql="INSERT INTO useraddress(userid,addName,diamondProvince,diamondCity,diamondDistrict,diamondStreet,addPhone,isDefault)\n" +
                    "VALUES(?,?,?,?,?,?,?,?)";
            count=this.executeUpdate(sql,param);
        }else {
            sql="INSERT INTO useraddress(userid,addName,diamondProvince,diamondCity,diamondDistrict,diamondStreet,addPhone,isDefault)\n" +
                    "VALUES(?,?,?,?,?,?,?,?)";
            count=this.executeUpdate(sql,param);
        }
        return count;
    }

    @Override
    public int addUpdate(Address address) {
        int count=0;
        try {
            String sql="UPDATE useraddress SET addName=?,diamondProvince=?,diamondCity=?,diamondDistrict=?,diamondStreet=?,addPhone=? WHERE useraddress.addressid=? and useraddress.userid=?\n";
            Object[] param={address.getAddName(), address.getDiamondProvince(), address.getDiamondCity(), address.getDiamondDistrict(), address.getDiamondStreet(), address.getAddPhone(), address.getAddressId() ,address.getUserId()};
            count=this.executeUpdate(sql,param);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int addDelete(int userid, int addressid) {
        int count=0;
        try {
            String sql="DELETE FROM useraddress WHERE useraddress.userid=? and useraddress.addressid=?";
            Object[] param={userid,addressid};
            count=this.executeUpdate(sql,param);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public List<Address> addSelect(int userid) {
        ResultSet rs=null;
        Address address=null;
        List<Address> addresses=new ArrayList<>();
        try {
            String sql="SELECT *\n" +
                    "FROM useraddress\n" +
                    "WHERE useraddress.userid=?";
            Object[] param={userid};
            rs=this.executeQuery(sql,param);
            while (rs.next()){
                address=new Address();
                address.setAddressId(rs.getInt(1));
                address.setUserId(rs.getInt(2));
                address.setAddName(rs.getString(3));
                address.setDiamondProvince(rs.getString(4));
                address.setDiamondCity(rs.getString(5));
                address.setDiamondDistrict(rs.getString(6));
                address.setDiamondStreet(rs.getString(7));
                address.setAddPhone(rs.getString(8));
                address.setIsDefault(rs.getInt(9));
                addresses.add(address);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return addresses;
    }
}
